<template>
  <div class="automation">
    <div class="page-header">
      <h2>自动化任务</h2>
      <el-button type="primary" @click="addTask">添加定时任务</el-button>
    </div>

    <el-table :data="tasks" style="width: 100%">
      <el-table-column prop="name" label="任务名称"></el-table-column>
      <el-table-column prop="deviceName" label="控制设备"></el-table-column>
      <el-table-column prop="time" label="执行时间"></el-table-column>
      <el-table-column prop="action" label="执行动作">
        <template #default="scope">
          {{ scope.row.action === 'on' ? '开启' : '关闭' }}
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template #default="scope">
          <el-switch
            :model-value="scope.row.enabled"
            @update:model-value="(val) => toggleTask(scope.row.id, val)"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button
            size="small"
            type="danger"
            @click="removeTask(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加定时任务对话框 -->
    <el-dialog
      title="添加定时任务"
      v-model="dialogVisible"
      width="30%">
      <el-form :model="taskForm" label-width="80px">
        <el-form-item label="任务名称">
          <el-input v-model="taskForm.name" placeholder="例如：晚上关灯"></el-input>
        </el-form-item>
        <el-form-item label="选择设备">
          <el-select v-model="taskForm.deviceId" placeholder="选择要控制的设备">
            <el-option
              v-for="device in devices"
              :key="device.id"
              :label="`${device.name} (${device.room})`"
              :value="device.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="执行时间">
          <el-time-picker
            v-model="taskForm.time"
            format="HH:mm"
            placeholder="选择时间">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="执行动作">
          <el-select v-model="taskForm.action" placeholder="选择动作">
            <el-option label="开启" value="on"></el-option>
            <el-option label="关闭" value="off"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmTask">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useStore } from 'vuex'

export default {
  name: 'Automation',
  setup() {
    const store = useStore()
    const dialogVisible = ref(false)
    const taskForm = ref({
      name: '',
      deviceId: '',
      time: null,
      action: ''
    })

    // 从 store 获取任务列表
    const tasks = computed(() => store.getters.automationTasks)

    // 从 store 获取所有设备列表
    const devices = computed(() => store.getters.allDevices)

    const addTask = () => {
      taskForm.value = {
        name: '',
        deviceId: '',
        time: null,
        action: ''
      }
      dialogVisible.value = true
    }

    const confirmTask = () => {
      if (taskForm.value.name && taskForm.value.deviceId && taskForm.value.time && taskForm.value.action) {
        const device = devices.value.find(d => d.id === taskForm.value.deviceId)
        const timeString = taskForm.value.time.getHours().toString().padStart(2, '0') + ':' + 
                         taskForm.value.time.getMinutes().toString().padStart(2, '0')
        
        store.dispatch('addAutomationTask', {
          ...taskForm.value,
          deviceName: `${device.name} (${device.room})`,
          time: timeString
        })
        
        dialogVisible.value = false
      }
    }

    const toggleTask = (taskId, enabled) => {
      store.dispatch('toggleAutomationTask', { taskId, enabled })
    }

    const removeTask = (taskId) => {
      store.dispatch('removeAutomationTask', taskId)
    }

    return {
      dialogVisible,
      taskForm,
      tasks,
      devices,
      addTask,
      confirmTask,
      toggleTask,
      removeTask
    }
  }
}
</script>

<style scoped>
.automation {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 